<template>
	<view>
		<view class="search-box flex-y-center">
			<image src="../../static/images/icon-search.png" mode=""></image>
			<input type="text" placeholder="请输入私教名称搜索" :auto-focus="true" class="input-text" confirm-type="search" @confirm="search" v-model="keyword"/>
		</view>
		
		<view class="search-history" v-if="!isSearch">
			<view class="row flex-x-bet-y-center">
				<view class="flex-y-center">
					<image src="../../static/images/icon-history.png" class="icon-history" mode=""></image>
					<text class="history-title">历史搜索</text>
				</view>
				<view class="clear-btn" @tap="clearHistory" v-if="historyList.length">清空</view>
			</view>
			<view class="hisotry-list">
				<view class="history flex-xy-center" v-for="(item, index) in historyList" @tap="searchByHistory(item)">{{item}}</view>
			</view>
		</view>
		
		<view class="teacher-list">
			<view class="teacher-item" v-for="(item, index) in tableData" @tap="navTo('/pages/teacher/detail?teacherId=' + item.teacherId)">
				<view class="info flex-x-bet-y-center">
					<view class="flex-y-center">
						<image src="../../static/images/default-avatar.png" v-if="!item.sysUser.avatar" class="avatar" mode=""></image>
						<image :src="item.sysUser.avatar | formatFile" v-else class="avatar" mode=""></image>
						<text class="name">{{item.sysUser.nickName}}</text>
					</view>
					<view class="tag-list flex-y-center">
						<view class="tag-item" v-for="(tag, index) in item.tagName">{{tag}}</view>
					</view>
				</view>
				<view class="intro">{{item.intro}}</view>
			</view>
		</view>
		<my-loadmore :currentStep="loadingStep" :marginBottom="60" v-if="isSearch && loadingStep != 0 && tableData.length"></my-loadmore>
		
		<my-empty :isShow="isSearch && loadingStep != 0 && !tableData.length"></my-empty>
		
		<my-modal ref="modal"></my-modal>
	</view>
</template>

<script>
	import { getTeacherList } from '@/api/common.js'
	export default {
		data() {
			return {
				keyword: '',
				isSearch: false,
				historyList: []
			};
		},
		onLoad(){
			this.historyList = uni.getStorageSync('searchHistory') || []
		},
		methods:{
			search(){
				this.initQuery()
				if(this.keyword && this.keyword.trim()){
					this.historyList.unshift(this.keyword)
					uni.setStorageSync('searchHistory', this.historyList)
					this.isSearch = true
					this.queryParams.nickName = this.keyword;
					this.queryList(getTeacherList, data => {
						data.forEach(item => {
							item.tagName = item.tagName.split(',')
						})
					})
				}else{
					this.isSearch = false
				}
			},
			searchByHistory(item){
				this.initQuery();
				this.keyword = item
				this.search()
			},
			clearHistory(){
				this.$refs.modal.open({
					content: '确定要清空吗？',
					success: () => {
						uni.removeStorageSync('searchHistory')
						this.historyList = []
					},
					cancel: () => {
						
					}
				})
			}
		}
	}
</script>

<style lang="scss">
	.search-box {
		display: flex;
		border: 2rpx solid $theme-color;
		border-radius: 36rpx;
		height: 73rpx;
		margin: 30rpx;
	
		image {
			width: 46rpx;
			height: 46rpx;
			margin: 0 15rpx 0 20rpx;
		}
	
		.input-text {
			font-size: 28rpx;
			flex: 1;
			margin-right: 30rpx;
		}
	}
	
	.search-history{
		padding: 30rpx 30rpx 0;
		
		image{
			width: 46rpx;
			height: 46rpx;
			margin-right: 15rpx;
		}
		
		.history-title{
			font-size: 32rpx;
			font-weight: 500;
			color: #333333;
		}
		
		.clear-btn{
			font-size: 26rpx;
			font-weight: bold;
			color: #EC1328;
		}
		
		.hisotry-list{
			display: flex;
			flex-wrap: wrap;
			
			.history{
				padding: 13rpx 30rpx;
				background: #F7F7F7;
				border-radius: 26rpx;
				font-size: 28rpx;
				font-weight: 500;
				color: #666666;
				margin-top: 30rpx;
				margin-right: 20rpx;
			}
		}
	}
	
	.teacher-list {
		padding: 30rpx;
		padding-top: 0;
	
		.teacher-item {
			padding: 40rpx 30rpx;
			margin-top: 30rpx;
			background: #FFFFFF;
			box-shadow: 0px 6rpx 35rpx 0px rgba(0, 0, 0, 0.1);
			border-radius: 26rpx;
	
			.info {
				display: flex;
			
				.avatar {
					width: 92rpx;
					height: 92rpx;
					margin-right: 30rpx;
					border-radius: 50%;
					flex-shrink: 0;
					
				}
			
				.name {
					font-size: 32rpx;
					font-weight: bold;
					color: #333333;
					flex-shrink: 0;
				}
			
				.tag-list {
					display: flex;
					flex-wrap: wrap;
					justify-content: flex-end;
					margin-left: 30rpx;
					
					.tag-item {
						padding: 4rpx 10rpx;
						background-color: $theme-color;
						font-size: 24rpx;
						color: #fff;
						border-radius: 6rpx;
						margin-right: 15rpx;
						margin-top: 10rpx;
					}
				}
			}
			
			
	
			.intro {
				padding-top: 20rpx;
				font-size: 28rpx;
				font-weight: 500;
				color: #666666;
				line-height: 44rpx;
				overflow : hidden;
				text-overflow: ellipsis;
				display: -webkit-box;
				-webkit-line-clamp: 5;
				-webkit-box-orient: vertical;
			}
		}
	}
</style>
